package com.ryan;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;

/**
 * Hello world!
 *
 */
public class App 
{
	
	public void copyFile(Manifest manifest, JarFile in, JarOutputStream out) throws Exception{
		byte[] buffer = new byte[1024 * 4];
		Map<String, Attributes> entries = manifest.getEntries();
		for(String name : entries.keySet()){
			JarEntry inEntry = in.getJarEntry(name);
			if(inEntry == null){
				continue;
			}
			if(inEntry.getMethod() == 0){
				out.putNextEntry(new JarEntry(inEntry));
			}else {
				out.putNextEntry(new JarEntry(name));
			}
			InputStream data = in.getInputStream(inEntry);
			int num;
			while((num = data.read(buffer)) > 0){
				out.write(buffer, 0, num);
			}
			out.flush();
		}
	}
	
	public void del(Manifest manifest, File folder, String path){
		File[] files = folder.listFiles();
		for(File f : files){
			if(f.isFile()){
				System.out.println(path +"/" + f.getName());
				manifest.getEntries().remove(path +"/" + f.getName());
			}else if(f.isDirectory()){
				del(manifest, f, path +"/" + f.getName());
			}
		}
	}
	
	public void jar(File sourceFile, JarOutputStream out){
		try{
			FileInputStream in = new FileInputStream(sourceFile);
			int b;
			byte[] buffer = new byte[1024];
			while((b = in.read(buffer)) != -1){
				out.write(buffer, 0, b);
			}
			in.close();
		}catch(Exception e){
			
		}finally{
			
		}
	}
	
    public static void main( String[] args ) throws Exception
    {
//    	String jarFile = "D:/android/20131227/Multimode_Unipay_base.jar";
//    	JarFile jar = new JarFile(jarFile);

//    	for(Enumeration entity = jar.entries(); entity.hasMoreElements();){
//    		
//    	}
    	
    	
    	App app = new App();
    	JarFile inputJar = new JarFile("D:/ConfigApk.apk");
    	Manifest manifest = inputJar.getManifest();
//    	String assets = "assets";
//    	String file = "libdecrypt.jar";
//    	manifest.getEntries().remove(assets+"/armeabi/"+file);
//    	manifest.getEntries().remove(assets+"/armeabi-v7a/"+file);
//    	manifest.getEntries().remove(assets+"/mips/"+file);
//    	manifest.getEntries().remove(assets+"/x86/"+file);
//    	String strPath = "d:/android/20131220/ConfigApk.apk";
//    	String file = "D:/android/20131223/GameSample_TouchPay_20131219.apk";
    	String file1 = "D:/ConfigApk_new.apk";
//    	JarFile inputJar = new JarFile(new File(file));
    	JarOutputStream outputJar = new JarOutputStream(new FileOutputStream(file1));
		manifest.getEntries().remove("assets/CMGC/Android.Sdk.TouchPay_22010_1782F43D5ADDBE0B1AD27BEC581BAA72.jar");
//    	boolean b = manifest.getEntries().containsKey("lib/armeabi-v7a");
//    	System.out.println("@@@@:"+b);
//    	for(String key : manifest.getEntries().keySet()){
//    		if(key.contains("lib/armeabi-v7a")){
//        		System.out.println(key);
//    			System.out.println("$$$$$$$$");
//    		}
//    	}
//    	Attributes att = manifest.getEntries().get("lib/armeabi-v7a/");
//    	String root = "D:/android/20140210/1390730531482/lib/";
		outputJar.putNextEntry(new JarEntry("assets/CMGC/Android.Sdk.TouchPay_24012_AC0123D8440D257A748E50A56AC3C061.jar"));
		app.jar(new File("D:/android/20140221/1392864793304/assets/CMGC/chPay_24012_AC0123D8440D257A748E50A56AC3C061.jar"), outputJar);

		
//    	File[] v7Files = new File(root +"armeabi-v7a").listFiles();
//    	if(v7Files != null){
//	    	for(File f : v7Files){
//	    		manifest.getEntries().remove("lib/armeabi-v7a/" + f.getName());
//	    	}
//    	}
//    	File[] mipsFiles = new File(root + "mips").listFiles();
//    	if(mipsFiles != null){
//	    	for(File f : mipsFiles){
//	    		manifest.getEntries().remove("lib/mips/" + f.getName());
//	    	}
//    	}
//    	File[] x86Files = new File(root + "x86").listFiles();
//    	if(x86Files != null){
//	    	for(File f : x86Files){
//	    		manifest.getEntries().remove("lib/x86/" + f.getName());
//	    	}
//    	}
//    	manifest.getEntries().remove("resources.arsc");
//    	for(File f : files){
//    		fStr = "lib/armeabi/" + f.getName();
//    		System.out.println(fStr);
//        	manifest.getEntries().remove(fStr);
//    	}
//    	String path = "assets";
//    	for(File f : files2){
//    		if(f.isFile()){
//				System.out.println(path  + "/" + f.getName());
//    			manifest.getEntries().remove(path + "/"+f.getName());
//    		}else if(f.isDirectory()){
//    			app.del(manifest, f, path + "/" + f.getName());
//    		}
//    	}
//    	File[] files3 = new File("D:/android/20131220/NBDMX40223001/res").listFiles();
//    	String path2 = "res";
//    	for(File f : files3){
//    		if(f.isFile()){
//				System.out.println(path2  + "/" + f.getName());
//    			manifest.getEntries().remove(path2 + "/"+f.getName());
//    		}else if(f.isDirectory()){
//    			app.del(manifest, f, path2 + "/" + f.getName());
//    		}
//    	}
//    	for(File f : files2){
//    		fStr = "assets/armeabi/" + f.getName();
//    		System.out.println(fStr);
//        	manifest.getEntries().remove(fStr);
//    	}

//		outputJar.putNextEntry(new JarEntry("lib/armeabi-v7a/gdbserver"));
//		app.jar(new File(root + "armeabi/gdbserver"), outputJar);
//		outputJar.putNextEntry(new JarEntry("lib/armeabi-v7a/libyodo1_sdk_4_game.so"));
//		app.jar(new File(root + "armeabi/libyodo1_sdk_4_game.so"), outputJar);
    	System.out.println("############");
    	
    	app.copyFile(manifest, inputJar, outputJar);
    	inputJar.close();
    	outputJar.close();
    }
}
